﻿using System;
using GraphProcessor;
using IQIGame.Onigao.Logic;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UIElements;

namespace IQIGame.Onigao.GamePlay.Editors
{
    public class SkillTargetSelectNode: IDGenerateNode
    {
        [Input("技能效果")] public SkillEffectiveConfig Input;
        [Input("Setter")] public TargetSelectSetPort setter;
        [Input("强制输入"), ShowAsDrawer] public bool forceInput;
        [Input("目标指示器"), ShowAsDrawer] public string indicatorConfig;

        [Output] public SkillEffectiveAction_ParamNum targetNum;
        
        protected virtual ETargetSelectType targetType { get; }
        protected virtual bool checkRelation => false;
        
        [SerializeField]
        private ETargetRelation targetRelation;
        public override void OnSetView(BaseNodeView view)
        {
            base.OnSetView(view);

            if (this.checkRelation)
            {
                var enumFlagsField = new EnumFlagsField("目标关系类型", targetRelation);
                enumFlagsField.RegisterValueChangedCallback(evt => { this.targetRelation = (ETargetRelation)evt.newValue; });
                view.controlsContainer.Add(enumFlagsField);
            }
            else
            {
                targetRelation = 0;
            }
        }
        
        protected override void Process()
        {
            base.Process();
            var rConfig = new TargetSelectConfig()
            {
                guid = this.GenerateID(),
                type = (int)this.targetType,
                targetRelation = (int)this.targetRelation,
                forceInput = this.forceInput,
                indicator = this.indicatorConfig,
                customConfig = this.CreateConfig(),
            };
            if (this.Input != null)
            {
                this.Input.target = rConfig;
            }

            if (this.setter != null)
            {
                this.setter?.setter?.Invoke(rConfig);
            }
            this.targetNum = new SkillEffectiveAction_ParamNum()
            {
                dataGuid = rConfig.guid,
                key = BattleConst.TargetNum
            };
        }

        protected virtual SkillCustomConfig CreateConfig()
        {
            return null;
        }
    }
}